package com.world.cat.controller.api.open;

import com.world.cat.enums.Platform;
import com.world.cat.model.generate.Auto;
import com.world.cat.pojo.req.ReqLogin;
import com.world.cat.pojo.res.ResAuto;
import com.world.cat.service.sys.UserService;
import com.world.common.generate.auto.QApi;
import com.world.common.pojo.Result;
import com.world.common.util.SessionUtil;
import com.world.common.util.SysUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by gang on 2016/12/24.
 */
@Controller
@RequestMapping(value = "/api/open", name = "Api开放接口")
public class ApiOpenController {
    private static final Logger logger = Logger.getLogger(ApiOpenController.class);
    @Resource
    private UserService userService;

    @RequestMapping(value = "/hello", name = "你好a")
    @ResponseBody
    @QApi(dataClass = ResAuto.class)
    public Object hello(String id, String token) throws Exception {
        System.out.println(SessionUtil.getApiUserName());
        System.out.println(SessionUtil.getUserName());
        String data = "你好";
        return new Result(true, "成功", data);
    }

    @RequestMapping(value = "/login", name = "token获取")
    @ResponseBody
    public Object loginJson(@RequestBody ReqLogin loginReq) throws Exception {
        if (SysUtil.isEmpty(loginReq.getUsername())) {
            return new Result(false, "用户名不能为空");
        }
        if (SysUtil.isEmpty(loginReq.getPassword())) {
            return new Result(false, "密码不能为空");
        }


        Result result = userService.login(loginReq.getUsername(), loginReq.getPassword(), Platform.MOBILE);
        return result;
    }
}
